package com.dk.shiro;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.seller.TbSeller;
import com.dk.service.seller.SellerService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class SellerRealm extends AuthorizingRealm {

    @Reference
    private SellerService sellerService;

    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证");
        // 判断用户
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String sellerId = token.getUsername();
        TbSeller seller = sellerService.SellerBySellerId(sellerId);
        System.out.println(seller.getSellerId());
        if (seller == null) {
            // 用户不存在
            return null;
        }
        if (!seller.getStatus().equals("1")) {
            return null;
        }
        return new SimpleAuthenticationInfo(seller,seller.getPassword(), ByteSource.Util.bytes(seller.getSalte()),"");
    }
}
